/**
 * Created by zhangbo on 27/01/14.
 */

var CesiumDrag = (function () {

    function _(cesiumViewer, opt) {
        var option = this._option = opt || {};
        var viewer = this._viewer = cesiumViewer;
        var scene = this._scene = viewer.scene;
        var camera = this._camera = viewer.camera;
        var handler = this._handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
//        var handler = this._handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        this._moveFaceCallback = null;
        this._moveVCallback = null;
        var target = this._target = null;
        var dragging = this._dragging = false;
        var self = this;
        handler.setInputAction(function (click) {
//            var pickedObject = scene.pick(click.position);
//            if (Cesium.defined(pickedObject)) {
//            	if(self._targets){
//            		self._target = null
//            		for(var i = 0; i < self._targets.length; i++){
//            			if(pickedObject.id === self._targets[i]){
//            				self._target = pickedObject.id;
//            				break;
//            			}
//            		}
//            	}
//            	if(self._target){
//                	self._target.billboard.scale = 1.2;
//                    dragging = true;
//                    self._scene.screenSpaceCameraController.enableRotate = false;
//            	}
//            }
        	if(Cesium.defined(self._target)){
        		dragging = true;
        		self._scene.screenSpaceCameraController.enableRotate = false;
        	}
        }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
        handler.setInputAction(function (movement) {
            if (dragging && Cesium.defined(self._target)) {
//            	self._target.position = self._camera.pickEllipsoid(movement.endPosition);
            	if(self._moveFaceCallback){
            		this._moveFaceCallback(self._camera.pickEllipsoid(movement.endPosition));
            	}
            	if(self._moveVCallback){
            		this._moveVCallback(movement.endPosition.y, movement.startPosition.y);
            	}
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        handler.setInputAction(function (click) {
//        	self._target.billboard.scale = 1;
        	if(self._complete){
        		self._complete(self._target);
        	}
            dragging = false;
            self._scene.screenSpaceCameraController.enableRotate = true;
        }, Cesium.ScreenSpaceEventType.LEFT_UP);
    };

    _.prototype.setTargets = function (t) {
        this._targets = t;
    };

    _.prototype.removeTarget = function (t) {
        this._dragging = false;
        this._moveFaceCallback = null;
        this._moveVCallback = null;
        this._targets = null;
    };
    
    _.prototype.moveFaceCallback = function (f) {
        this._moveFaceCallback = f;
    };
    
    _.prototype.moveVCallback = function (f) {
        this._moveVCallback = f;
    };

    _.prototype.destroy = function () {
        this._target = null;
        this._handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN);
        this._handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this._handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP);
        this._handler.destroy();
        this._handler = null;
    };

    return _;
})();
